package com.hzt.system.controller;

import com.hzt.system.common.*;
import com.hzt.system.domain.User;
import com.hzt.system.service.UserService;
import com.hzt.system.vo.UserVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 *
 *
 * @Author:hzt
 * @Date:8:54 下午 2020/7/15
 */
@RestController
@RequestMapping("api/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("loadAllUser")
    public DataGridView loadAllUser(UserVo userVo){
          return userService.loadAllUser(userVo);
    }




    /**
     * 要放入缓存的
     *  @param user
     * @return
     */
    @PostMapping("addUser")
    public ResultObj addUser(User user){
        try {
            user.setSalt(MD5Utils.createUUID());
            user.setType(Constant.USER_TYPE_NORMAL);
            //设置密码
            user.setPwd(MD5Utils.md5(Constant.DEFAULT_PWD,user.getSalt(),2));
            user.setAvailable(Constant.AVAILABLE_TRUE);
            user.setImgpath(Constant.DEFAULT_TIT_IMAGE);
            this.userService.saveUser(user);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    //更新操作
    @RequestMapping("updateUser")
    public ResultObj updateUser(User user){
        try {
            this.userService.updateUser(user);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }


    //删除
    @RequestMapping("deleteUser")
    public ResultObj deleteUser(User user){
        try {
            this.userService.removeById(user.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }


    //查询最大排序码
    @GetMapping("queryUserMaxOrderNum")
    public Object queryDeptMaxOrderNum(){
        Integer  maxValue=this.userService.queryUserMaxOrderNum();
        return new DataGridView(maxValue+1);
    }

    //
    @RequestMapping("resetUserPwd")
    public ResultObj resetUserPwd(Integer id){
        try {
            User user = new User();
            user.setId(id);
            user.setSalt(MD5Utils.createUUID());
            user.setPwd(MD5Utils.md5(Constant.DEFAULT_PWD,user.getSalt(),2));
            this.userService.updateUser(user);
            return ResultObj.RESET_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.RESET_ERROR;
        }

    }


    @RequestMapping("saveUserRole")
    public ResultObj saveUserRole(Integer uid,Integer[] rids){
        try {
            this.userService.saveUserRole(uid,rids);
            return ResultObj.DISPATCH_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DISPATCH_ERROR;
        }
    }


    @GetMapping("currentUser")
    public DataGridView currentUser(){
        ActiveUser principal = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        return new DataGridView(principal.getUser());
    }



}
